<?php


namespace app\admin\logic;
use app\admin\error\Menu as MenuError;

/**
 * 菜单逻辑
 */
class MenuLogic extends AdminBase
{
    
    // 面包屑
    public static $crumbs       = [];
    
    // 菜单Select结构
    public static $menuSelect   = [];


    /**
     * 获取菜单列表
     */
    public static function getMenuList($data)
    {
        $where = empty($data['pid']) ? ['pid' => 0] : ['pid' => $data['pid']];
        $where['module'] = 'admin';
        return self::model('Menu')->getMenuList($where);
    }

    /**
     *菜单列表tree
     */
    public static function getRecursiveMenuList($params){

        $where = [];

        !empty($params['search_data']) && $where[] = ['name','like', '%'.$params['search_data'].'%'];

        $where[] = ['module','=','admin'];
        $data =  self::model('Menu')->getMenuList($where);
        !empty($params['search_data'])?$returnData = $data : $returnData = self::tree($data);
        return $returnData;

    }

    public static function tree($menuList,$pid=0){
        $returnData = [];
        if(!empty($menuList)){
            $num = 0;
            foreach ($menuList as $key => $value) {
                if($value['pid']==$pid){#条件
                    $returnData[$num]=$value;#公式
                    $returnData[$num]['children']=self::tree($menuList,$value['id']);#调用
                    if(empty( $returnData[$num]['children'])){
                        unset( $returnData[$num]['children']);
                    };

                    $num++;
                }
            }
        }

        return $returnData;#归值
    }

    /**
     *角色分配权限菜单列表tree
     */
    public static function getRolesRecursiveMenuList($params){

        $admin = Oauth::$userInfo;

        return $admin['allow_auth'];

    }

    /**
     * 获取菜单信息
     */
    public static function getMenuInfo($where = [], $field = true)
    {
        $where[] = ['module','=','admin'];
        $where[] = ['status','<>','-1'];
        return  self::model('Menu')->getInfo($where, $field);
    }

    /**
     * 菜单添加
     */
    public static function menuAdd($data = [])
    {
        self::paramSceneCheck(self::validate("menu"), "add",$data);

        $data['module'] = 'admin';

        $where[] = ['name','=',$data['name']];
        $where[] = ['module','=',$data['module']];
        $where[] = ['status','<>','-1'];
        $info =  self::model('Menu')->getInfo($where, '*');
        !empty($info)? self::adminError(MenuError::$nameRepeated) : '';

        $result = self::model("Menu")->addInfo($data);

        return $result ? $result : self::adminError(MenuError::$addError);
    }

    /**
     * 菜单编辑
     */
    public static function menuEdit($data = [])
    {

        self::paramSceneCheck(self::validate("menu"), "edit",$data);


        $where[] = ['module','=','admin'];
        $where[] = ['name','=',$data['name']];
        $where[] = ['id','<>',$data['id']];
        $where[] = ['status','<>','-1'];

        $info =  self::model('Menu')->getInfo($where, '*');
        !empty($info)? self::adminError(MenuError::$nameRepeated) : '';

        $updateWhere['id'] = $data['id'];

        $result =self::model("Menu")->updateInfo($updateWhere,$data);

        return $result ? $result : self::adminError(MenuError::$updateError);
    }

    /**
     * 菜单删除
     */
    public static function menuDel($data = [])
    {
        self::paramSceneCheck(self::validate("menu"), "delete",$data);

        $infoWhere[] = ['module','=','admin'];
        $infoWhere[] =  ['pid','=',$data['id']];
        $infoWhere[] = ['status','<>','-1'];
        $info =  self::model('Menu')->getInfo($infoWhere, '*');
        !empty($info)? self::adminError(MenuError::$hasSonError) : '';

        $where['module'] = 'admin';
        $where['id'] = $data['id'];


        $result = self::model("Menu")->getOneValue($where,DATA_STATUS_NAME, DATA_DELETE);;

        return $result ? $result: self::adminError(MenuError::$deleteError);
    }

}
